object FunctionVsMethodDemo extends App {
  case class Student(id: String, name: String, var className: String, scores: Map[String, Int])

  val students = scala.collection.mutable.ListBuffer[Student](
    Student("s2025001", "张三", "高一（1）班", Map("数学" -> 85, "语文" -> 90)),
    Student("s2025002", "李四", "高一（1）班", Map("数学" -> 92, "语文" -> 88)),
    Student("s2025003", "王五", "高一（2）班", Map("数学" -> 76, "语文" -> 82))
  )

  class StudentManager {
    def findStudentById(studentId: String): Option[Student] = {
      students.find(_.id == studentId)
    }

    def updateStudentClass(studentId: String, newClass: String): Boolean = {
      findStudentById(studentId) match {
        case Some(student) =>
          student.className = newClass
          true
        case None =>
          false
      }
    }
  }

  val calculateStudentAverage: Student => Double = (student) => {
    if (student.scores.isEmpty) 0.0
    else student.scores.values.sum.toDouble / student.scores.size
  }

  val getStudentGrade: Student => String = (student) => {
    val avg = calculateStudentAverage(student)
    if (avg >= 90) "优秀"
    else if (avg >= 80) "良好"
    else if (avg >= 70) "中等"
    else if (avg >= 60) "及格"
    else "不及格"
  }

  val manager = new StudentManager()

  val student = manager.findStudentById("s2025002")
  student match {
    case Some(s) => println(s"找到学生：${s.name},班级：${s.className}")
    case None => println("未找到学生")
  }

  val updateSuccess = manager.updateStudentClass("s2025002","高一（3）班")
  println(s"更新班级成功：$updateSuccess")
  manager.findStudentById("s2025002").foreach(s =>
    println(s"更新后班级：${s.className}")
  )

  students.foreach { s =>
    val avg = calculateStudentAverage(s)
    val grade = getStudentGrade(s)
    println(f"${s.name} 的平均分：$avg%.1f,等级：$grade")
  }

  val findFunc = manager.findStudentById _
  val result = findFunc("s2025001")
  println(s"\n通过函数查找：${result.map(_.name)}")
}
//第十周（11.03【动手练习3】：实现学生信息管理功能